var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    buthiddenNext: false,
    buthiddenSubmit:false,
    nextShow:true,
    telephone:"",
    code: "",
    verifyCodeTime:"发送验证码"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
  },
  //手机号前端校验
  onPhoneEvent: function(e) {
    var that = this;
    if (e.detail.value.length == 1) {
      that.setData({ telephone: e.detail.value.replace(/[^1-9]/g, '')});
    } else {
      that.setData({ telephone: e.detail.value.replace(/\D/g, '') }); 
    }
  },
  //手机号验证码前端校验
  onCodeEvent: function (e) {
    var that = this;
    that.setData({ code: e.detail.value.replace(/\D/g, '') });
  },

  //提交手机号
  onPhoneNext: function () {
    var that = this;
    var telephone = that.data.telephone;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var c = 60;
    if (telephone == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
      })
      return false
    }else if (telephone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }else if (!myreg.test(telephone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }else{
      that.setData({ buthiddenNext: false});

      var intervalId = setInterval(function () {

        c = c - 1;
        that.setData({
          verifyCodeTime: c + 's后重发',
          buthiddenNext: true
        })
        if (c == 0) {
          clearInterval(intervalId);
          that.setData({
            verifyCodeTime: '获取验证码',
            buthiddenNext: false
          })
        }
      }, 1000)

      wx.request({
        method: "POST",
        url: 'https://www.zaishengwang.com/wx/toSmsMessage.do',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          telephone: that.data.telephone,
        },
        success: function (res) {
          if (res.data.msg == "2") {

            wx.showModal({
              title: '该手机号已注册',
              confirmColor: '#56BDE0',
              showCancel: false,
            });
            clearInterval(intervalId);
            that.setData({
              verifyCodeTime: '获取验证码',
              buthiddenNext: false
            })
          } else if (res.data.msg == "3") {

            wx.showModal({
              title: '今日已超过次数，请明天再尝试',
              confirmColor: '#56BDE0',
              showCancel: false,
            });
            clearInterval(intervalId);
            that.setData({
              verifyCodeTime: '获取验证码',
              buthiddenNext: false
            })
          }else{
            that.setData({ buthiddenNext: true });
          }
        },
        fail: function (error) {
          that.setData({buthiddenNext: true });
        }
      })
    }
  },

  onPhoneSubmit:function(e){
    var that = this;
    var telephone = that.data.telephone;
    var code = that.data.code;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
   
    if (telephone == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
      })
      return false
    } else if (telephone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (!myreg.test(telephone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (code == '') {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none',
      })
      return false
    } else if (code.length != 4) {
      wx.showToast({
        title: '验证码长度有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }else{
    that.setData({ buthiddenSubmit: false });
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/toVerifyCode.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        phone: telephone,
        code:code,
        userId: app.globalData.userId,
      },
      success: function (res) {
        var c = 3;
        if(res.data.msg=="2"){
          wx.showModal({
            title: '验证码错误',
            confirmColor: '#56BDE0',
            showCancel: false,
          })
        } else if (res.data.msg == "3") {
          wx.showModal({
            title: '请输入正确手机号',
            confirmColor: '#56BDE0',
            showCancel: false,
          })
        }else if (res.data.msg == "4") {
          wx.showModal({
            title: '验证码不存在',
            confirmColor: '#56BDE0',
            showCancel: false,
          })
          that.setData({ buthiddenNext: false });
        } else if (res.data.msg == "1") {
          that.setData({ buthiddenSubmit: true });
          wx.showToast({
            title: '手机号修改成功',
            icon: 'success',
            duration: 1500
          });
          var intervalId = setInterval(function () {
            c = c - 1;
            if (c == 0) {
              clearInterval(intervalId);
              wx.navigateBack({
                delta: 1
              })
            }
          }, 1000)
        }
      },
      fail: function (error) {
        that.setData({ buthiddenSubmit: false });
      }
    })

    }
  }, 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})